package com.kasax.base

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Process
import com.blankj.utilcode.util.Utils
import com.kasax.utils.AppActivityManager

open class BaseApp : Application() {
    override fun onCreate() {
        super.onCreate()

        Utils.init(this)
        handler = Handler()
        looper = mainLooper
        myPid = Process.myPid()
        mThreadId = Process.myTid()
        mThreadName = Thread.currentThread().name
        appContext = this

        registerActivityLifecycleCallbacks(object : DefActLifecycle() {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                super.onActivityCreated(activity, savedInstanceState)
                //全局强制竖屏
                activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                AppActivityManager.instance.addActivity(activity)
            }

            override fun onActivityDestroyed(activity: Activity) {
                super.onActivityDestroyed(activity)
                AppActivityManager.instance.removeActivity(activity)
            }
        })
    }

    companion object {
        var appContext: Context? = null
        var mThreadId: Int = -1
        var mThreadName: String? = ""
        var handler: Handler? = null
        var looper: Looper? = null
        var myPid: Int = -1
    }
}